/**
 *  转换ant table的sorter对象转换成分页的查询的排序字段
 * @param {object} sorter
 */
export const conventSorter = sorter => {
    const option = {};
    if(sorter)
        if(sorter.order && sorter.field) {
            // 点击排序
            option.sortField = sorter.field;
            if(sorter.order === 'descend') option.isDesc = true;
            else option.isDesc = false;
        } else {
            // 取消排序
            option.sortField = undefined;
            option.isDesc = undefined;
        }
    return option;
};
export const hasPermissions = (data, name) => Array.isArray(data) && data.length > 0 && data.some(item => item === name);

export const categoryTypeCompare = (a, b) => (a.level || 0) - (b.level || 0);

export const availableTypes = (types, typeId) => {
    const {level = 0} = types.find(item => item.id === typeId) || {};
    if(!level)
        return types.map(item => ({
            text: item.name,
            value: item.id
        }));
    return types.filter(p => p.level >= level).map(item => ({
        text: item.name,
        value: item.id
    }));
};

export const sorter = (key, a, b) => {
    if(a[key] > b[key])
        return 1;
    if(a[key] < b[key])
        return -1;
    return 0;
};

/**
 * [getObjById 根据ID获取树上对应id的对象]
 * @param  {[Array]} data [树的数据]
 * @param  {[String]} id   [需要获取的id]
 * @return {[Object]}      [对象]
 */
export const getObjById = (data, id) => {
    if(!data || !data.length || !id) return {};
    const newData = [];
    for(let i = 0; i < data.length; i++) {
        /*eslint-disable  eqeqeq*/
        if(data[i].id == id)
            return data[i];
        if(data[i].items)
            newData.push(...data[i].items);
    }
    return getObjById(newData, id);
};

/**
 * [getPathById 根据ID获取树上对应id的对象]
 * @param  {[Array]} data [树的数据]
 * @param  {[String]} id   [需要获取的id]
 * @return {[Array]}      [path]
 */
export const getPathById = (data, id) => {
    for(let i = 0; i < data.length; i++) {
        const organization = data[i];
        /*eslint-disable  eqeqeq*/
        if(organization.id == id)
            return [i];
        if(organization.items && organization.items.length > 0) {
            const path = getPathById(organization.items, id);
            if(path)
                return [i, 'items'].concat(path);
        }
    }
};


import {formatMessage} from './intl';
export const validateAddCategory = data => {
    const {id, name, code, typeId} = data;
    const msgs = [];
    if(!code)
        msgs.push(
            formatMessage({
                id: 'utils.validateAddCategory.code',
                defaultMessage: '编号'
            })
        );
    if(!name)
        msgs.push(
            formatMessage({
                id: 'utils.validateAddCategory.name',
                defaultMessage: '名称'
            })
        );
    if(!id && !typeId)
        msgs.push(
            formatMessage({
                id: 'utils.validateAddCategory.typeId',
                defaultMessage: '类型'
            })
        );
    if(msgs.length)
        return `${msgs.join(
            formatMessage({
                id: 'utils.validateAddCategory.comma',
                defaultMessage: '、'
            })
        )}${formatMessage({
            id: 'utils.validateAddCategory.notEmpty',
            defaultMessage: '不能为空'
        })}`;
    return false;
};


export const validateRepairMode = data => {
    const {id, name, code} = data;
    const msgs = [];
    if(!id && !code)
        msgs.push(
            formatMessage({
                id: 'utils.validateRepairMode.code',
                defaultMessage: '编号'
            })
        );
    if(!id && !name)
        msgs.push(
            formatMessage({
                id: 'utils.validateRepairMode.name',
                defaultMessage: '名称'
            })
        );
    if(msgs.length)
        return `${msgs.join(
            formatMessage({
                id: 'utils.validateRepairMode.comma',
                defaultMessage: '、'
            })
        )}${formatMessage({
            id: 'utils.validateRepairMode.notEmpty',
            defaultMessage: '不能为空'
        })}`;
    return false;
};

export const validateAdd = (data = []) => {
    if(!data.length)
        return formatMessage({
            id: 'utils.data.length',
            defaultMessage: '维修项目不能为空'
        });
    const msgs = [];
    let nameFlag = false;
    let codeFlag = false;
    let laborHourFlag = false;
    for(let i = 0; i < data.length; i++) {
        const {name, code, laborHour} = data[i];
        if(!code && !codeFlag) {
            codeFlag = true;
            msgs.push(
                formatMessage({
                    id: 'utils.validateAdd.code',
                    defaultMessage: '维修项目编号'
                })
            );
        }
        if(!name && !nameFlag) {
            nameFlag = true;
            msgs.push(
                formatMessage({
                    id: 'utils.validateAdd.name',
                    defaultMessage: '维修项目名称'
                })
            );
        }
        if(!laborHour && !laborHourFlag && laborHour !== 0) {
            laborHourFlag = true;
            msgs.push(
                formatMessage({
                    id: 'utils.validateAdd.laborHour',
                    defaultMessage: '工时'
                })
            );
        }
    }
    if(msgs.length)
        return `${msgs.join(
            formatMessage({
                id: 'utils.validateAdd.comma',
                defaultMessage: '、'
            })
        )}${formatMessage({
            id: 'utils.validateRepairMode.notEmpty',
            defaultMessage: '不能为空'
        })}`;
    return false;
};
